package com.gs.synedocket.presenter;

import com.google.gson.reflect.TypeToken;
import com.gs.synedocket.apiresult.ApiResultData;
import com.gs.synedocket.apiresult.PagedList;
import com.gs.synedocket.db.dto.DocketDto;
import com.gs.synedocket.db.dto.DocketSummaryDto;
import com.gs.synedocket.db.dto.ShortDto;
import com.gs.synedocket.db.query.BaseQuery;
import com.gs.synedocket.db.query.DocketQuery;
import com.gs.synedocket.model.ComboModel;
import com.gs.synedocket.model.DocketModel;
import com.gs.synedocket.model.listener.OnResultListener;
import com.gs.synedocket.view.IDocketLogView;

import java.lang.reflect.Type;
import java.util.List;

public class DocketLogPresenter extends BasePresenter<IDocketLogView> {

    private DocketModel docketModel = new DocketModel();
    private ComboModel comboModel = new ComboModel();

    public void docketsLog(DocketQuery query, String what) {
        if (getView() != null) {
            Type typeofT = new TypeToken<ApiResultData<PagedList<DocketDto>>>() {
            }.getType();
            docketModel.docketsLog(query,what, new OnResultListener() {
                @Override
                public void onSuccess(Object aipResult) {
                    if (getView() != null) {
                        getView().onGetDataSuccess((ApiResultData<PagedList<DocketDto>>) aipResult);
                        getView().hideLoading();
                    }
                }

                @Override
                public void onFails(Exception e) {
                    if (getView() != null) {
                        getView().onGetDataFail(e);
                        getView().hideLoading();
                    }
                }
            }, typeofT);
        }
    }

    public void getSummary(String truckNo) {
        if (getView() != null) {
            Type typeofT = new TypeToken<ApiResultData<DocketSummaryDto>>() {
            }.getType();
            docketModel.getSummary(truckNo,new OnResultListener() {
                @Override
                public void onSuccess(Object aipResult) {
                    if (getView() != null) {
                        getView().onGetSummarySuccess((ApiResultData<DocketSummaryDto>) aipResult);
                    }
                }

                @Override
                public void onFails(Exception e) {
                    if (getView() != null) {
                        getView().onGetSummaryFail(e);
                    }
                }
            }, typeofT);
        }
    }

    public void combo(String typeKey) {
        getView().showLoading();
        comboModel.combo(typeKey,new OnResultListener() {
            @Override
            public void onSuccess(Object apiResult) {
                if (getView() != null) {
                    getView().onGetComListSuccess((ApiResultData<List<ShortDto>>) apiResult);
                    getView().hideLoading();
                }
            }

            @Override
            public void onFails(Exception e) {
                if (getView() != null) {
                    getView().onGetComListFails(e);
                    getView().hideLoading();
                }
            }
        }, new TypeToken<ApiResultData<List<ShortDto>>>() {
        }.getType());
    }
}
